package cn.iocoder.yudao.module.system.controller.admin.officeinfo;

import cn.iocoder.yudao.module.system.dal.dataobject.seatreserve.SeatReserveDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.officeinfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.officeinfo.SeatinfoDO;
import cn.iocoder.yudao.module.system.service.officeinfo.SeatinfoService;

@Tag(name = "管理后台 - 办公室座位信息")
@RestController
@RequestMapping("/system/seatinfo")
@Validated
public class SeatinfoController {

    @Resource
    private SeatinfoService seatinfoService;

    @PostMapping("/create")
    @Operation(summary = "创建办公室座位信息")
    @PreAuthorize("@ss.hasPermission('system:seatinfo:create')")
    public CommonResult<Long> createSeatinfo(@Valid @RequestBody SeatinfoSaveReqVO createReqVO) {
        return success(seatinfoService.createSeatinfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新办公室座位信息")
    @PreAuthorize("@ss.hasPermission('system:seatinfo:update')")
    public CommonResult<Boolean> updateSeatinfo(@Valid @RequestBody SeatinfoSaveReqVO updateReqVO) {
        seatinfoService.updateSeatinfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除办公室座位信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:seatinfo:delete')")
    public CommonResult<Boolean> deleteSeatinfo(@RequestParam("id") Long id) {
        seatinfoService.deleteSeatinfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得办公室座位信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:seatinfo:query')")
    public CommonResult<SeatinfoRespVO> getSeatinfo(@RequestParam("id") Long id) {
        SeatinfoDO seatinfo = seatinfoService.getSeatinfo(id);
        return success(BeanUtils.toBean(seatinfo, SeatinfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得办公室座位信息分页")
    @PreAuthorize("@ss.hasPermission('system:seatinfo:query')")
    public CommonResult<PageResult<SeatinfoRespVO>> getSeatinfoPage(@Valid SeatinfoPageReqVO pageReqVO) {
        PageResult<SeatinfoDO> pageResult = seatinfoService.getSeatinfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SeatinfoRespVO.class));
    }

    @GetMapping("/list-by-officeidAndDate")
    @Operation(summary = "获得办公室座位信息分页")
    @PreAuthorize("@ss.hasPermission('system:seatinfo:query')")
    public CommonResult<List<SeatinfoRespVO>> getSeatinfoByofficeidAndDate(@Valid SeatinfoPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SeatinfoDO> pageResult = seatinfoService.getSeatinfoPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, SeatinfoRespVO.class));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出办公室座位信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:seatinfo:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSeatinfoExcel(@Valid SeatinfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SeatinfoDO> list = seatinfoService.getSeatinfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "办公室座位信息.xls", "数据", SeatinfoRespVO.class,
                        BeanUtils.toBean(list, SeatinfoRespVO.class));
    }

}